# coding:utf-8
__author__ = "ila"

from flask import session, jsonify, request
from . import main_bp
from utils.codes import *
from utils.jwt_auth import Auth
from api.models.brush_model import *

@main_bp.route("/python758879l3/<table>/page", methods=['GET'])
def python758879l3_product_list(table):
    if request.method == 'GET':
        msg = {"code": normal_code, "msg": "success", "data": {}}
        req_dict = session.get("req_dict")

        model_map = {
            'bijiben': bijiben,
            'diannaozhuji': diannaozhuji,
            'yingjianzuzhuang': yingjianzuzhuang,
            'diannaowaishe': diannaowaishe
        }
        
        if table not in model_map:
            msg['code'] = crud_error_code
            msg['msg'] = "无效的表名"
            return jsonify(msg)

        model = model_map[table]
        msg['data']['list'], msg['data']['currPage'], msg['data']['totalPage'], msg['data']['total'], \
        msg['data']['pageSize'] = model.page(model, model, req_dict)

        return jsonify(msg)

@main_bp.route("/python758879l3/<table>/info/<id_>", methods=['GET'])
def python758879l3_product_info(table, id_):
    if request.method == 'GET':
        msg = {"code": normal_code, "msg": "success", "data": {}}

        model_map = {
            'bijiben': bijiben,
            'diannaozhuji': diannaozhuji,
            'yingjianzuzhuang': yingjianzuzhuang,
            'diannaowaishe': diannaowaishe
        }
        
        if table not in model_map:
            msg['code'] = crud_error_code
            msg['msg'] = "无效的表名"
            return jsonify(msg)

        model = model_map[table]
        data = model.getbyid(model, model, int(id_))
        if len(data) > 0:
            msg['data'] = data[0]
        return jsonify(msg)

@main_bp.route("/python758879l3/<table>/save", methods=['POST'])
def python758879l3_product_save(table):
    if request.method == 'POST':
        msg = {"code": normal_code, "msg": "success"}
        req_dict = session.get("req_dict")

        model_map = {
            'bijiben': bijiben,
            'diannaozhuji': diannaozhuji,
            'yingjianzuzhuang': yingjianzuzhuang,
            'diannaowaishe': diannaowaishe
        }
        
        if table not in model_map:
            msg['code'] = crud_error_code
            msg['msg'] = "无效的表名"
            return jsonify(msg)

        model = model_map[table]
        error = model.createbyreq(model, model, req_dict)
        if error != None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return jsonify(msg)

@main_bp.route("/python758879l3/<table>/update", methods=['POST'])
def python758879l3_product_update(table):
    if request.method == 'POST':
        msg = {"code": normal_code, "msg": "success"}
        req_dict = session.get("req_dict")

        model_map = {
            'bijiben': bijiben,
            'diannaozhuji': diannaozhuji,
            'yingjianzuzhuang': yingjianzuzhuang,
            'diannaowaishe': diannaowaishe
        }
        
        if table not in model_map:
            msg['code'] = crud_error_code
            msg['msg'] = "无效的表名"
            return jsonify(msg)

        model = model_map[table]
        error = model.updatebyparams(model, model, req_dict)
        if error != None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return jsonify(msg)

@main_bp.route("/python758879l3/<table>/delete", methods=['POST'])
def python758879l3_product_delete(table):
    if request.method == 'POST':
        msg = {"code": normal_code, "msg": "success"}
        req_dict = session.get("req_dict")

        model_map = {
            'bijiben': bijiben,
            'diannaozhuji': diannaozhuji,
            'yingjianzuzhuang': yingjianzuzhuang,
            'diannaowaishe': diannaowaishe
        }
        
        if table not in model_map:
            msg['code'] = crud_error_code
            msg['msg'] = "无效的表名"
            return jsonify(msg)

        model = model_map[table]
        error = model.delete(model, req_dict)
        if error != None:
            msg['code'] = crud_error_code
            msg['msg'] = error
        return jsonify(msg)